import {
	home
} from '@/api'
import {
	ref,
	reactive
} from 'vue'
import {
	onLoad
} from "@dcloudio/uni-app"
let params = reactive({
	skip: '0',
	limit: '4'
})
let competitiveList = ref([])
let startOrEnd = reactive({
	start: 0,
	end: 0
})
let count = ref(0)
// 左滑右滑
function touchEnd(e) {
	if (e.changedTouches.length > 0) {
		startOrEnd.end = parseInt(e.changedTouches[0].clientX)
		let diff = startOrEnd.start < startOrEnd.end
		if (diff) {
			console.log('右滑')
		} else {
			if(competitiveList.value.length >= count.value) {
				uni.$u.toast('没有更多了')
				return 
			}
			getCompetitive()
		}
	}
}

function touchStart(e) {
	if (e.changedTouches.length > 0) {
		startOrEnd.start = parseInt(e.changedTouches[0].clientX)
	}
}

function getCompetitive() {
	params.skip = competitiveList.value.length
	home.reqCompetitive(params).then(res => {
		if (res.code === 1) {
			let data = competitiveList.value
			data.push(...res.data)
			competitiveList.value = data
			count.value = res.count
		}
	})
}

export default function() {
	onLoad(() => {
		getCompetitive()
	})

	return {
		competitiveList,touchEnd,touchStart
	}
}
